package com.transaction.demo.trade.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.common.util.JsonUtils;
import com.transaction.demo.common.util.ResultPage;
import com.transaction.demo.trade.biz.TradePaymentRecordBiz;
import com.transaction.demo.trade.dao.TradePaymentRecordDao;
import com.transaction.demo.trade.entity.TradePaymentRecordBean;
import com.transaction.demo.trade.enums.TradeStatusEnum;
import com.transaction.demo.trade.vo.req.InitPaymentOrderReqVo;
import com.transaction.demo.trade.vo.req.TradePaymentRecordListReqVo;
import com.transaction.demo.trade.vo.req.TradePaymentRecordReqVo;
import com.transaction.demo.trade.vo.req.TradePaymentRecordUpdateReqVo;
import com.transaction.demo.trade.vo.resp.TradePaymentRecordRespVo;

/**
 * 描述：支付记录表
 * 作者：马庆智
 * 日期：2023/04/27 17:22:10
 */
@Slf4j
@Service
public class TradePaymentRecordBizImpl implements TradePaymentRecordBiz {

	@Autowired
	private TradePaymentRecordDao tradePaymentRecordDao;

	/**
	 * 查询支付记录表
	 *
	 * @param tradePaymentRecord 请求参数
	 * @return TradePaymentRecordResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public TradePaymentRecordRespVo queryTradePaymentRecord(TradePaymentRecordReqVo tradePaymentRecord) {

		TradePaymentRecordBean query = tradePaymentRecordDao.queryTradePaymentRecord(
				TradePaymentRecordBean.builder().bankOrderNo(tradePaymentRecord.getBankOrderNo()).build());

        TradePaymentRecordRespVo recordRespVo = new TradePaymentRecordRespVo();
        BeanUtils.copyProperties(query,recordRespVo);
        return recordRespVo;
	}

	/**
	 * 查询支付记录表列表
	 *
	 * @param tradePaymentRecord 请求参数
	 * @return TradePaymentRecordResp
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public ResultPage<TradePaymentRecordRespVo> queryTradePaymentRecordList(
			TradePaymentRecordListReqVo tradePaymentRecord) {

		PageHelper.startPage(tradePaymentRecord.getCurrent(), tradePaymentRecord.getPageSize());
		List<TradePaymentRecordBean> query = tradePaymentRecordDao.queryTradePaymentRecordList(
				TradePaymentRecordBean.builder().merchantOrderNo(tradePaymentRecord.getMerchantOrderNo())
						.bankOrderNo(tradePaymentRecord.getBankOrderNo()).build());
		PageInfo<TradePaymentRecordBean> pageInfo = new PageInfo<>(query);

		List<TradePaymentRecordRespVo> list = pageInfo.getList().stream().map(x -> {
			TradePaymentRecordRespVo resp = new TradePaymentRecordRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加支付记录表
	 *
	 * @param vo 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public int saveTradePaymentRecord(InitPaymentOrderReqVo vo) {
		TradePaymentRecordBean recordBean = new TradePaymentRecordBean();
		recordBean.setId(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		recordBean.setVersion(1);
		recordBean.setCreateTime(new Date());
		recordBean.setStatus(TradeStatusEnum.WAITING_PAYMENT.name());
		recordBean.setEditor("koobe");
		recordBean.setCreater("koobe");
		recordBean.setEditTime(new Date());
		recordBean.setProductName("下单测试");
		recordBean.setMerchantOrderNo(vo.getBusinessId());
		recordBean.setTrxNo(new Random().toString());
		recordBean.setBankOrderNo(vo.getBusinessId());
		recordBean.setBankTrxNo("123");
		recordBean.setMerchantName("123");
		recordBean.setMerchantNo(vo.getBusinessId());
		recordBean.setPayerUserNo("123");
		recordBean.setPayerName("123");
		recordBean.setPayerPayAmount("100");
		recordBean.setPayerFee("1");
		recordBean.setPayerAccountType("123");
		recordBean.setReceiverUserNo("123");
		recordBean.setReceiverName("123");
		recordBean.setReceiverPayAmount("1");
		recordBean.setReceiverFee("1");
		recordBean.setReceiverAccountType("123");
		recordBean.setOrderIp("123");
		recordBean.setOrderRefererUrl("123");
		recordBean.setOrderAmount("1");
		recordBean.setPlatIncome("1");
		recordBean.setFeeRate("1");
		recordBean.setPlatCost("123");
		recordBean.setPlatProfit("123");
		recordBean.setReturnUrl("https://www.baidu.com");
		recordBean.setNotifyUrl("http://10.168.96.23:8100/order/v1/notify");
		recordBean.setPayWayCode("123");
		recordBean.setPayWayName("123");
		recordBean.setPaySuccessTime(new Date());
		recordBean.setCompleteTime(new Date());
		recordBean.setIsRefund("0");
		recordBean.setRefundTimes(0);
		recordBean.setSuccessRefundAmount("1");
		recordBean.setTrxType("123");
		recordBean.setOrderFrom("123");
		recordBean.setPayTypeCode("");
		recordBean.setPayTypeName("");
		recordBean.setFundIntoType("");
		recordBean.setRemark(vo.getRemark());
		recordBean.setBankReturnMsg("");

		log.info("添加支付记录信息：{}", JsonUtils.toJson(recordBean));
		return tradePaymentRecordDao.saveTradePaymentRecord(recordBean);
	}

	/**
	 * 删除支付记录表
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public int deleteTradePaymentRecord(String ids) {
		return tradePaymentRecordDao.deleteTradePaymentRecord(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新支付记录表
	 *
	 * @param tradePaymentRecord 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/27 17:22:10
	 */
	@Override
	public int updateTradePaymentRecord(TradePaymentRecordUpdateReqVo tradePaymentRecord) {
        TradePaymentRecordBean bean = new TradePaymentRecordBean();
        BeanUtils.copyProperties(tradePaymentRecord,bean);
        return tradePaymentRecordDao.updateTradePaymentRecord(bean);
	}

}